<template>
    <div class="equipment-clear"
         style="height: 100%;">
        <vue-better-scroll class="wrapper"
                           ref="scroll"
                           :scrollbar="scrollbarObj"
                           :pullDownRefresh="pullDownRefreshObj"
                           :pullUpLoad="pullUpLoadObj"
                           :startY="parseInt(startY)"
                           @pullingUp="onPullingUp">
            <ul class="nav">
                <li :class="{'active':navIndex === 0}"
                    @click="navIndex = 0;getData()">
                    <p>活动记录</p>
                </li>
                <li :class="{'active':navIndex === 1}"
                    @click="navIndex = 1;getData()">
                    <p>排行榜</p>
                </li>
            </ul>
            <div v-show="navIndex === 0">
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>单位名称</label>
                    </div>
                    <div class="equipment-right-select"
                         @click="selectType">
                        <span>{{checkcompany0.Name?checkcompany0.Name:'请选择'}}</span>
                        <i class="iconfont icon-jinru rightmsg"></i>
                    </div>
                </div>
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>查询时间</label>
                    </div>
                    <group>
                        <datetime v-model="startTime0"
                                  format="YYYY-MM"></datetime>
                    </group>
                </div>
                <div class="btns">
                    <!-- <button>保存</button> -->
                    <button class="green width200"
                            @click="getData">查询</button>
                </div>
                <div class="cut-off-rule"></div>
                <h3>活动总数：{{listAllCount0}}</h3>
                <ul class="lists">
                    <li v-for="(item,index) in lists0"
                        :key="index">
                        <router-link :to="{path:'/safetyDetail',query:{detail:JSON.stringify(item)}}">
                            <div class="describe">
                                <h4>{{item.ActivityName}}</h4>
                                <div class="tags">
                                    <span>活动时间：{{item.ActivityTime}}</span>
                                    <span class="yellow"
                                          @click.prevent="giveLike(item)">
                                        <i class="iconfont icon-damuzhi"></i>{{item.GiveThumbsup}}
                                    </span>
                                </div>
                            </div>
                            <div class="cover">
                                <!-- <img src="@/assets/defaultImg.png" /> -->
                                <img v-if="item.FileName[0]"
                                     :src="item.FileName[0]"
                                     alt="">
                                <img v-else
                                     src="@/assets/defaultImg.png" />
                            </div>
                        </router-link>
                    </li>
                </ul>
            </div>
            <div v-show="navIndex === 1">
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>单位层级</label>
                    </div>
                    <!-- <div class="equipment-right-select"
                         @click="selectType">
                        <span>{{checkcompany1.Name?checkcompany1.Name:'请选择'}}</span>
                        <i class="iconfont icon-jinru rightmsg"></i>
                    </div> -->
                    <group>
                        <popup-picker :data="companyLayer"
                                      placeholder='默认全部'
                                      v-model="checkcompany1"></popup-picker>
                    </group>
                </div>
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>排名指标</label>
                    </div>
                    <group>
                        <popup-picker :data="equipmentType"
                                      placeholder='默认全部'
                                      v-model="equipmentTypeVal0"></popup-picker>
                    </group>
                </div>
                <div class="form-item form-item-style1">
                    <div class="label-title">
                        <label>开始时间</label>
                    </div>
                    <group>
                        <datetime v-model="startTime1"
                                  format="YYYY-MM"></datetime>
                    </group>
                </div>
                <div class="btns">
                    <!-- <button>保存</button> -->
                    <button class="green width200"
                            @click="getData">查询</button>
                </div>
                <div class="cut-off-rule"></div>
                <ul class="lists">
                    <li v-for="(item,index) in lists1"
                        :key="index"
                        class="padding">
                        <h4>{{item.DepartName}}</h4>
                        <p>活动次数：{{item.ASVCount}}次</p>
                    </li>
                </ul>
            </div>
        </vue-better-scroll>
        <myMenu></myMenu>
    </div>
</template>
<script>
import VueBetterScroll from 'vue2-better-scroll'
import formatDate from '@/utils/formatDate'
import myMenu from '@/commonComponents/menu'
export default {
    name: 'safetyIndex',
    data () {
        return {
            scrollbarObj: {
                fade: true
            },
            // 这个配置用于做下拉刷新功能，默认为 false。当设置为 true 或者是一个 Object 的时候，可以开启下拉刷新，可以配置顶部下拉的距离（threshold） 来决定刷新时机以及回弹停留的距离（stop）
            pullDownRefreshObj: false,
            // 这个配置用于做上拉加载功能，默认为 false。当设置为 true 或者是一个 Object 的时候，可以开启上拉加载，可以配置离底部距离阈值（threshold）来决定开始加载的时机
            pullUpLoadObj: {
                threshold: 0,
                txt: {
                    more: '加载更多',
                    noMore: '没有更多数据了'
                }
            },
            startY: 0, // 纵轴方向初始化位置
            navIndex: 0,
            startTime0: `${formatDate().split('-')[0]}-${formatDate().split('-')[1]}`,
            startTime1: `${formatDate().split('-')[0]}-${formatDate().split('-')[1]}`,
            userInfo: '',
            checkcompany0: {
                Name: '全部',
                Id: ''
            }, //  单位名称
            lists0: [],
            lists1: [],
            pageIndex0: 0, // 当前页
            listAllCount0: 0, // 总条数
            pageIndex1: 0, // 当前页
            listAllCount1: 0, // 总条数
            equipmentType: [['活动次数', '参加人数', '点赞次数']],
            equipmentTypeVal0: ['活动次数'],
            companyLayer: [['全部', '项目层级', '分子公司层级']],
            checkcompany1: ['全部'] //  单位名称
        }
    },
    components: {
        VueBetterScroll,
        myMenu
    },
    created () {
        this.userInfo = JSON.parse(localStorage.getItem('userInfo')) || {}

        this.checkcompany0.Name = this.userInfo.deptname
        this.checkcompany0.Id = this.userInfo.deptid
        if (this.navIndex == 0) {
            this.handleSubmit0()
        } else {
            this.handleSubmit1()
        }
    },
    activated () {
        this[`checkcompany${this.navIndex}`] = this.$store.state.checkcompany && JSON.stringify(this.$store.state.checkcompany) !== '{}' ? JSON.parse(JSON.stringify(this.$store.state.checkcompany)) : {
            Name: this.userInfo.deptname,
            Id: this.userInfo.deptid
        }
    },
    methods: {
        selectType () {
            this.$router.push({
                path: '/testing/lookcompany',
                query: {
                    all: '全部'
                }
            })
        },
        onButtonClick (type) {
            alert('on button click ' + type)
        },
        handleEvents (type) {
            console.log('event: ', type)
        },
        handleSubmit0 () {
            this.$vux.loading.show({
                text: '加载中...'
            })
            let params = {
                'allowPaging': false,
                'business': 'GetActivityRecordList',
                'data': {
                    'projectID': this[`checkcompany${this.navIndex}`].Id,
                    'selectTime': this.startTime0
                },
                'pageIndex': this.pageIndex0,
                'pageSize': 20,
                'tokenId': '',
                'userId': this.userInfo.userid
            }
            this.$axiosAjax.getConfigJson(params).then((res) => {
                if (res.data.code === 0) {
                    this.listAllCount0 = res.data.count
                    this.lists0 = this.lists0.concat(res.data.data)
                    if (this.lists0.length < this.listAllCount0) {
                        this.$refs.scroll.forceUpdate(true)
                    } else {
                        this.$refs.scroll.forceUpdate(false)
                    }
                }
            }).catch(() => {

            }).finally(() => {
                this.$vux.loading.hide()
            })
        },
        handleSubmit1 () {
            this.$vux.loading.show({
                text: '加载中...'
            })
            let params = {
                'allowPaging': true,
                'business': 'GetRankingCount',
                'data': {
                    'projectID': this[`checkcompany${this.navIndex}`][0] == '全部' ? '' : this[`checkcompany${this.navIndex}`][0],
                    'rankingIndex': this.equipmentTypeVal0[0],
                    'selectTime': this.startTime1
                },
                'pageIndex': 0,
                'pageSize': 20,
                'tokenId': '',
                'userId': this.userInfo.userid
            }
            this.$axiosAjax.getConfigJson(params).then((res) => {
                if (res.data.code === 0) {
                    this.listAllCount1 = res.data.count
                    this.lists1 = this.lists1.concat(res.data.data)
                    if (this.lists1.length < this.listAllCount1) {
                        this.$refs.scroll.forceUpdate(true)
                    } else {
                        this.$refs.scroll.forceUpdate(false)
                    }
                }
            }).catch(() => {

            }).finally(() => {
                this.$vux.loading.hide()
            })
        },
        onPullingUp () {
            this[`pageIndex${this.navIndex}`]++
            this[`handleSubmit${this.navIndex}`]()
        },
        getData () {
            this[`pageIndex${this.navIndex}`] = 0
            this[`lists${this.navIndex}`] = []
            this[`handleSubmit${this.navIndex}`]()
        },
        giveLike (item) {
            let params = {
                'allowPaging': true,
                'business': 'GetGiveThumbsupNum',
                'data': {
                    'ID': item.ID
                },
                'pageIndex': 1,
                'pageSize': 20,
                'tokenId': '6D04B564963A96592ACCB34BBAB35509',
                'userId': this.userInfo.userid
            }
            this.$axiosAjax.getConfigJson(params).then((res) => {
                if (res.data.code === 0) {
                    item.GiveThumbsup = res.data.data
                }
            }).catch(() => {

            }).finally(() => {
                this.$vux.loading.hide()
            })
        }
    }
}
</script>
<style lang="less" scoped>
.nav {
    display: flex;
    padding-bottom: 10px;
    background: #ebeef3;
    li {
        display: flex;
        justify-content: center;
        flex: 1;
        background: #fff;
        &.active p {
            color: #2269ff;
            border-bottom: 4px solid #2269ff;
        }
    }
    p {
        height: 60px;
        line-height: 60px;
        text-align: center;
        font-size: 18px;
        color: #999;
    }
}
h3 {
    height: 45px;
    line-height: 45px;
    margin-left: 20px;
    font-size: 14px;
    color: #2269ff;
    border-bottom: 1px solid #efeff4;
}
.lists {
    padding-left: 20px;
    li {
        border-bottom: 1px solid #efeff4;
        &.padding {
            padding: 14px 20px 14px 0;
        }
    }
    a {
        display: flex;
        justify-content: space-between;
        padding: 14px 20px 14px 0;
    }
    .describe {
        flex: 1;
    }
    h4 {
        line-height: 32px;
        color: #333;
        font-size: 14px;
        font-weight: 600;
        margin-bottom: 10px;
    }
    .tags {
        display: flex;
        justify-content: space-between;
        line-height: 32px;
        span {
            color: #666;
            font-size: 14px;
        }
        span:last-child {
            display: flex;
            color: #2268fe;
            i {
                color: #2268fe;
                margin-right: 3px;
            }
        }
    }
    .cover {
        margin-left: 32px;
        img {
            display: block;
            width: 100px;
            height: 73px;
        }
    }
}
</style>
